package com.slipper.core.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExcelReadListener extends AnalysisEventListener<Map<Integer, String>> {
    private final List<List<Map<Integer, String>>> sheetDataList = new ArrayList<>();
    private final List<Map<Integer, String>> headList = new ArrayList<>();

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        headList.add(headMap);
    }

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        List<Map<Integer, String>> dataList;
        if (sheetDataList.size() > context.readSheetHolder().getSheetNo()) {
            dataList = sheetDataList.get(context.readSheetHolder().getSheetNo());
        } else {
            dataList = new ArrayList<>();
            sheetDataList.add(dataList);
        }

        dataList.add(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }

    public List<Map<Integer, String>> getHeadList() {
        return headList;
    }

    public List<List<Map<Integer, String>>> getSheetDataList() {
        return sheetDataList;
    }
}